package com.cloud.rou.im.router.context;

import com.cloud.rou.im.cluster.naming.node.ImServerInstance;
import com.cloud.rou.im.core.util.SingletonUtil;
import lombok.Getter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2019-11-13 21:50
 */
@Getter
public class ImServerInstanceContext {

    private Map<Long,ChannelAndInstance> instanceMap=new ConcurrentHashMap<>();

    private ImServerInstanceContext(){

    }
    public static ImServerInstanceContext getInstance(){
        return SingletonUtil.get(ImServerInstanceContext.class);
    }

    public boolean add(ChannelAndInstance instance){

        instanceMap.putIfAbsent(instance.getInstance().getId(),instance);

        return !instanceMap.containsKey(instance.getInstance().getId());
    }
    public void remove(ImServerInstance instance){
        instanceMap.remove(instance.getId());
    }
    public void remove(Long id){
        instanceMap.remove(id);
    }
}
